﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace Platformer1
{
    class Fireball
    {
      public Texture2D texture;
        public Vector2 origin;
        //private SoundEffect collectedSound;

        public Animation mymovementanimation;
        public AnimationPlayer fball;

        public int iX;
        public bool bActive = true;
        public float fElapsed = 0f;
        public float fUpdateInterval = 0.015f;
        public int iSpeed = 8;
        public SpriteEffects flip;

        public int duration = 40;
        public string type = "";

        public int direction = 1;

        public const int PointValue = 30;
        //public readonly Color Color = Color.Red;

        // The powerup is animated from a base position along the Y axis.
        public Vector2 position;

        public Level Level
        {
            get { return level; }
        }
        public Level level;

        public Vector2 Position
        {
            get
            {
                return position;
            }
        }

        /// <summary>
        /// Gets a circle which bounds this powerup in world space.
        /// </summary>
        public Circle BoundingCircle
        {
            get
            {
                return new Circle(Position, Tile.Width / 3.0f);
            }
        }

        public Fireball()
        {
        }

        /// <summary>
        /// Constructs a new powerup.
        /// </summary>
        public Fireball(Level level, Vector2 position, SpriteEffects inflip)
        {
            this.level = level;
            this.position = position;
            this.flip = inflip;

            LoadContent();
        }

        /// <summary>
        /// Loads the powerup texture and collected sound.
        /// </summary>
        public virtual void LoadContent()
        {
            texture = Level.Content.Load<Texture2D>("Sprites/fireball2");
            origin = new Vector2(texture.Width / 2.0f, texture.Height / 2.0f);
            mymovementanimation = new Animation(texture, .01f, true);
            fball = new AnimationPlayer();
            //collectedSound = Level.Content.Load<SoundEffect>("Sounds/PowerUp");
        }

        public virtual void Update(GameTime gameTime)
        {
            fElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (fElapsed > fUpdateInterval)
            {
                duration--;
                fElapsed = 0f;
                if (this.flip == SpriteEffects.None )
                {
                    position.X += iSpeed;
                }
                else
                {
                    position.X -= iSpeed;
                }
               
                if (duration <= 0)
                {
                    bActive = false;
                }

            }

        //fElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;
        //if (fElapsed > fUpdateInterval)
        //{
        //    fElapsed = 0f;
        //    //if (iFacing == 0)
        //    //{
        //        iX += iSpeed;
        //    //}
        //    //    iX -= iSpeed;
        //    //}

        //    // If the bullet has moved off of the screen,
        //    // set it to inactive
        //    if ((iX > 1280) || (iX < 0))
        //    {
        //        bActive = false;
        //    }
        //}
        }

        /// <summary>
        /// Called when this powerup has been collected by a player and removed from the level.
        /// </summary>
        /// <param name="collectedBy">
        /// The player who collected this powerup. Although currently not used, this parameter would be
        /// useful for creating special powerup powerups. For example, a powerup could make the player invincible.
        /// </param>
        /*public void OnCollected(Player collectedBy)
        {
            collectedSound.Play();
        }*/

        /// <summary>
        /// Draws a powerup in the appropriate color.
        /// </summary>
        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (bActive)
            {
                //spriteBatch.Draw(, new Vector2 (position.X, position.Y), Color.White);
                fball.PlayAnimation(mymovementanimation);
                fball.Draw(gameTime, spriteBatch, position, flip);
            }
        }
    }
}
